﻿using System;
using UnityEngine;
using BridgeShips.GUISystem;
using System.Collections.Generic;
using UnityEngine.UI;

namespace BridgeShips.Control {
    public class BuildingHelpUI : GUIBehaviour {

        [SerializeField]
        private Window m_Window;

        [SerializeField]
        private Camera m_GUICamera;

        [Header( "Audio" )]
        [SerializeField]
        private SoundPlayer m_RefreshAudio;

        [SerializeField]
        private SoundPlayer m_SelectButtonAudio;

        private BuildingCategory[] m_Categories;

        private Button btn;

        private void Update() {
            if (!m_Window.IsOpen)
                return;

            float scrollValue = Player.ScrollValue.Get();
          
        }

        private void Start() {
            Player.Help.AddStartTryer( TryStart_HelpUI );
            Player.Help.AddStopTryer( TryStop_HelpUI );

            InventoryController.Instance.State.AddChangeListener( OnChanged_InventoryState );

            //btn = transform.GetChild( 2 ).GetComponent<Button>();
            btn = transform.Find( "ExitButton" ).GetComponent<Button>();
            btn.onClick.AddListener( BtnClick );
        }

        public bool isVisable() {
            return Player.LineCharts.Active;
        }

        private void OnChanged_InventoryState() {
            var inventoryClosed = InventoryController.Instance.IsClosed;
            if (!inventoryClosed) {
                while (Player.Help.Active)
                    Player.Help.TryStop();
            }
        }

        private bool TryStart_HelpUI() {
            if (!InventoryController.Instance.IsClosed)
                return false;
            m_Window.Open();
            return true;
        }

        private bool TryStop_HelpUI() {
            m_Window.Close();
            return true;
        }

        void BtnClick() {
            PlayerInputHandler.curActivity = null;
            GameController.LocalPlayer.Help.TryStop();
        }
    }
}
